package scala.for_the.impatient

/**
  * Created by shichen on 2017/5/24.
  */

trait Logger {
  def log(msg: String)
  //特质方法不一定是抽象的
  def log2(msg: String): Unit = {
    println(msg)
  }
}

trait TimestampLogger extends Logger {
  abstract override def log(msg: String): Unit = {
    //Logger.log方法并未实现，如果不加abstract,override关键字，
    //不能通过编译
    super.log(new java.util.Date() + " " + msg)
  }
}

//编译器把extends后面的东西是看成整体的
class ConsoleLogger extends Logger /*with Cloneable with Serializable */{
  def log(msg: String): Unit = {
    println(msg)
    log2(msg)
  }
}

/*
//trait继承类，不常见的用法
trait LoggedException extends Exception with Logger {
  def log() = log(getMessage)
}
class UnhappyException extends  LoggedException {
  //override def getMessage: String = "hello"

  override def log(msg: String): Unit = {

  }
}*/

object ch10_trait extends App{

  def t_trait(): Unit = {
    val logger = new ConsoleLogger with TimestampLogger
    logger.log("hello")
  }
}
